﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;

namespace ProjectStudy.Ajax
{
    /// <summary>
    /// Ajax_PageACLManage 的摘要说明
    /// </summary>
    public class Ajax_PageACLManage : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action= context.Request["action"];
            string result = "";
            switch (action)
            {
                case "GetPageACLList": result = GetPageACLList(context);
                    break;
                case "UpdatePageACL": result = UpdatePageACL(context);
                    break;
                case "DelPageACL": result = DelPageACL(context);
                    break;
                case "AddPageACL": result = AddPageACL(context);
                    break;
            }
            context.Response.Write(result);
        }

        private string AddPageACL(HttpContext context)
        {
            string pageId=context.Request["pageId"];
            string url = context.Request["Url"];
            string group=context.Request["Group"];
            int rightClass =Convert.ToInt32( context.Request["RightClass"]);
            string sql = "";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            sql = "select 1 from PageACL where PageId=@pageid";
            SqlParameter ps1 = new SqlParameter("@pageid",pageId);
            DataTable dt = DBAccess.ExecuteDataTable(sql, ps1);
            if (dt.Rows.Count > 0)
            {
                dic.Add("status", "inserduplicate");
            }
            else
            {
                sql = "insert into PageACL values(@PageId,@PageURL,@PageUserGroup,@PageRightClass)";
                SqlParameter[] ps2 = { 
                                     new SqlParameter("@PageId",pageId),
                                     new SqlParameter("@PageURL",url),
                                     new SqlParameter("@PageUserGroup",group),
                                     new SqlParameter("@PageRightClass",rightClass)
                                     };
                int a = DBAccess.ExecuteNonQuery(sql, ps2);
                if (a > 0)
                {
                    dic.Add("status", "success");
                }
                else
                {
                    dic.Add("status","insertErr");
                }
            }
            return JsonConvert.SerializeObject(dic);
        }

        private string DelPageACL(HttpContext context)
        {
            string[] pageid = context.Request["PageId"].Replace("[", "").Replace("]", "").Replace("\"", "").Split(',');
            string sql = "";
            string err = "";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (pageid.Length > 0)
            {
                for (int i = 0; i < pageid.Length; i++)
                {
                    sql = "delete from PageACL where pageId=@pageid";
                    SqlParameter ps = new SqlParameter("@pageid",pageid[i]);
                    int a = DBAccess.ExecuteNonQuery(sql,ps);
                    if (a <= 0)
                    {
                        err = err + "," + pageid[i];
                    }
                }
                if (err.Length > 0)
                {
                    dic.Add("status", "updateErr");
                    dic.Add("errMsg", err);
                }
                else
                {
                    dic.Add("status", "success");
                }
            }
            else
            {
                dic.Add("status", "noData");
            }
            return JsonConvert.SerializeObject(dic);
        }

        private string UpdatePageACL(HttpContext context)
        {
            string[] pageid = context.Request["PageId"].Replace("[","").Replace("]","").Replace("\"","").Split(',');
            string sql = "";
            string err = "";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (pageid.Length > 0)
            {
                string group = context.Request["group"];
                int Rightclass = Convert.ToInt32(context.Request["class"]);
                for (int i = 0; i < pageid.Length; i++)
                {
                    sql = "update PageACL set pageUserGroup=@group,PageRightClass=@class where PageId=@pageid";
                    SqlParameter[] ps = { 
                                         new SqlParameter("@group",group),
                                         new SqlParameter("@class",Rightclass),
                                         new SqlParameter("@PageId",pageid[i])
                                        };
                    int a = DBAccess.ExecuteNonQuery(sql, ps);
                    if (a <= 0)
                    {
                        err = err + "," + pageid[i];
                    }
                }
                if (err.Length > 0)
                {
                    dic.Add("status", "updateErr");
                    dic.Add("errMsg", err);
                }
                else
                {
                    dic.Add("status","success");
                }
            }
            else
            {
                dic.Add("status", "noData");
            }
            return JsonConvert.SerializeObject(dic);
        }

        private string GetPageACLList(HttpContext context)
        {
            string group=context.Request["group"];
            string sql = "";
            DataTable dt = new DataTable();
            if (group == "all")
            {
                sql = "select * from PageACL";
                dt = DBAccess.ExecuteDataTable(sql);
            }
            else
            {
                sql = "select * from PageACL where PageUserGroup=@group";
                SqlParameter ps = new SqlParameter("@group",group);
                dt = DBAccess.ExecuteDataTable(sql, ps);
            }
            return JsonConvert.SerializeObject(dt);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}